package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.AllocationOutChooseInOrderActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.AllocationOutCreatDocumentActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutStorageBaseInfoBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * 生产入库单列表
 *
 * @author 詹建宇
 * @time on 2018/3/29 14:18
 * @email jianyu.zhan@inossem.com
 */
public class AllocationChooseInOrderAdapter extends BaseAdapter {

    private Activity context;
    private List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> list;
    private String mCondition;
    private AllocationOutChooseInOrderActivity.SearchListener listener;

    public AllocationChooseInOrderAdapter(Activity context, String mCondition, List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> list, AllocationOutChooseInOrderActivity.SearchListener listener) {
        this.context = context;
        this.list = list;
        this.mCondition = mCondition;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (i == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.cy_view_out_storage_allocation_inorder_first, null);
            EditText condition = (EditText) view.findViewById(R.id.keyword);
            ImageView search = (ImageView) view.findViewById(R.id.search);
            condition.setText(mCondition + "");
            condition.setHint("请输入生产入库单号");
            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(context, condition);
                    listener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(context, v);
                    listener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            ViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.cy_view_out_storage_allocation_inorder_item, null);
                holder = new ViewHolder();
                holder.codeTv = (TextView) view.findViewById(R.id.codeTv);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean bean = list.get(i - 1);
            holder.codeTv.setText(bean.getStock_input_code());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.putExtra(AllocationOutStorageExtra.ALLOCATION_INORDER_LIST_BEAN, bean);
                    context.setResult(AllocationOutCreatDocumentActivity.RESULTCODE, intent);
                    context.finish();
                }
            });
        }
        return view;
    }

    private class ViewHolder {
        private TextView codeTv;
    }

}
